import store from '../../store'
var requestHandler = {
	url: '',
	data: {},
	method: '',
	success: function(res) {



	},
	fail: function() {},
	complete: function() {}
}

function requests(requestHandler) {
	var data = requestHandler.data || {};
	var url = requestHandler.url;


	var method = requestHandler.method;
	var header = requestHandler.header;
	// wx.showLoading({
	// 	title: '加载中',
	// })

	//store.dispatch('showloading')
	uni.showLoading({
		title: '正在加载'
	})
	uni.request({
		url: url,
		data: data,
		method: method,
		header: header,
		success: function(res) {

			if (res.data.code != 0) {

				if (url.indexOf('liteWeChatLogin') > -1) {
					uni.showToast({
						title: '请授权登录',
						icon: 'none',
						duration: 5000
					})
				} else {

					uni.showModal({
						title: '温馨提示',
						content: res.data.message,
						showCancel: false, //是否显示取消按钮
						success: function(res) {
							if (res.cancel) {
								//点击取消,默认隐藏弹框
							} else {

							}
						}

					})
				}
				if (res.data.code == 100027) {
					setTimeout(function() {
						uni.navigateBack({
							delta: -1
						})
					}, 1000)
				}

			}
			requestHandler.success(res)
			uni.hideLoading()
			//store.dispatch('hideloading');
		},
		fail: function() {
			//store.dispatch('hideloading');
			uni.hideLoading()
			requestHandler.fail();
		},
		complete: function() {
			uni.hideLoading()
			//store.dispatch('hideloading');
		}

	})
}

export {
	requests
}
